package com.zh.kotlin_mvvm.ui

import android.annotation.SuppressLint
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.database.getLongOrNull
import androidx.core.database.getStringOrNull
import com.blankj.utilcode.util.LogUtils
//import com.xyz.informercial.base.BusiAd
//import com.xyz.informercial.entity.DisLikeBean
//import com.xyz.informercial.listener.OnBackListener
//import com.xyz.informercial.utils.BusiSpUtils
import com.zh.common.ktx.toJson
import com.zh.kotlin_mvvm.ScreenType
import com.zh.kotlin_mvvm.databinding.ActivityTextAdBinding
import com.zh.kotlin_mvvm.provider.DisLikeBean

class TextAdActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityTextAdBinding.inflate(layoutInflater)
    }

    @SuppressLint("Range")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        queryAll {
            LogUtils.i("2 -> ${toJson(it)}")
        }

        binding.apply {
            btn1.setOnClickListener {
//                BusiAd.insertScreenAd(
//                    this@TextAdActivity,
//                    ScreenType.insertPlacementId,//广告位id(必填)
//                    ScreenType.insertSceneId,//广告场景id(必填)
//                    back = object : OnBackListener {
//
//                    }//回调监听(必填)
//                )
            }
        }

        val CONTENT_URI = Uri.parse("content://com.example.myapp.provider/items")
        // 查询数据
        val cursor = contentResolver.query(CONTENT_URI, null, null, null, null)
        if (cursor != null) {
            while (cursor.moveToNext()) {
                val name = cursor.getString(cursor.getColumnIndex("name"))
                Log.e("sd", "queryName -> $name")
            }
            cursor.close()
        }
    }

    @SuppressLint("Range")
    fun queryAll(callback: (List<DisLikeBean>) -> Unit = {}) {
        val list = mutableListOf<DisLikeBean>()
        val uri = Uri.parse("content://com.xyz.xyin_fomercial.provider/ads")
        val contentProvider = contentResolver.query(uri, null, null, null, null)
        if (contentProvider != null) {
            contentProvider.apply {
                while (moveToNext()) {
                    val id = getInt(getColumnIndex("id"))
                    val adId = getStringOrNull(getColumnIndex("adId"))
                    val timestamp = getLongOrNull(getColumnIndex("timestamp"))
//                    list.add(DisLikeBean(id, adId, timestamp))
                }
                close()
                callback.invoke(list)
            }
        } else {
            callback.invoke(mutableListOf())
        }
    }
}